﻿using Common;
using FreeSql.Context;
using FreeSql.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace enterprise.Website.Core.ApiControllers
{
    [Route("[controller]")]
    [ApiController]
    public class AjaxController : ControllerBase
    {
        private readonly SqlContext _sqlContext;
        private readonly BaseWebElement _baseWebElement;
        public AjaxController(BaseWebElement baseWebElement,
            SqlContext sqlContext)
        {
            _baseWebElement = baseWebElement;
            _sqlContext = sqlContext;
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetVerification")]
        public IActionResult GetVerification()
        {
            var VerificationStr = HttpContext.Session.GetString(SystemConstConfig.Verification);
            if (!VerificationStr.IsNullOrWhiteSpace())
            {
                HttpContext.Session.Remove(SystemConstConfig.Verification);
            }
            var Verification = CheckCodeHelper.GenerateCheckCode();
            HttpContext.Session.SetString(SystemConstConfig.Verification, Verification.ToLower());
            var CheckCodeImage = CheckCodeHelper.CreateCheckCodeImage(Verification);
            return File(CheckCodeImage, "image/Gif");
        }

        /// <summary>
        /// 推送留言信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("PostMessage")]
        public async Task<ApiResult> PostMessage(MessageModel data)
        {
            var result = new ApiResult { Success = false, Message = "Error" };
            if (data != null)
            {
                var Verification= HttpContext.Session.GetString(SystemConstConfig.Verification);
                if (Verification.ToLower() != data.verifyCode.ToLower())
                {
                    result.Message = _baseWebElement.QueryLanText("VerificationError", data.language);
                    return result;
                }
                var id = await _sqlContext.AddAsync<Message_Info>(new Message_Info
                {
                    Name = data.name,
                    Phone = data.phone,
                    Content = data.content,
                    Company = data.company,
                    Email = data.email,
                    CreateTime = DateTime.Now.ToString()
                });
                if (id > 0)
                {
                    result.Success = true;
                    result.Message = _baseWebElement.QueryLanText("SubmitSuccess", data.language);
                }
            }
            return result;
        }

        /// <summary>
        /// 发送询盘信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost("PostInquiry")]
        public async Task<ApiResult> PostInquiry(MessageModel data)
        {
            var result = new ApiResult { Success = false, Message = "Error" };
            if (data != null)
            {
                var id = await _sqlContext.AddAsync<Message_Info>(new Message_Info
                {
                    Name = "",
                    Phone = "",
                    Company = data.company,
                    Content = data.content,
                    Email = data.email,
                    CreateTime = DateTime.Now.ToString()
                });
                if (id > 0)
                {
                    result.Success = true;
                    result.Message = _baseWebElement.QueryLanText("SubmitSuccess", data.language);
                }
            }
            return result;
        }
    }
}
